<!--{include file='public/top' /}-->

<body>
<div class="x-body">

    <form action="" method="post" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="x-red">*</span>角色名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="name" name="name" required="" value="<?php echo $data['name'];?>" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>

        <hr>
        <div style=""><h2>角色权限</h2> <br></div>
        <div class="layui-form-item layui-form-text">
            <?php foreach($menu_list_arr as $val):?>
            <label class="layui-form-label">
                <?php echo $val['name'];?>
            </label>

            <table class="layui-table layui-input-block">
                <tbody>
                <?php foreach($val['menu'] as $menu):?>
                <tr>
                    <td width="200px">
                        <input type="checkbox" name="power[]" lay-skin="primary" title="<?php echo $menu['name'];?>" value="<?php echo $menu['id'];?>" id="<?php echo $menu['id'];?>" <?php if($menu['is_checked']==true):?>checked<?php endif;?>>
                        <div class="layui-unselect layui-form-checkbox" lay-skin="primary">
                            <span><?php echo $menu['name'];?></span>
                            <i class="layui-icon"></i>
                        </div>
                    </td>
                    <td>
                        <div class="layui-input-block">
                            <?php if(isset($menu['sub']) && !empty($menu['sub'])):?>
                                <?php foreach($menu['sub'] as $sub):?>
                                    <span>
                                        <input name="power[]" lay-skin="primary" type="checkbox" data-parent="<?php echo $sub['parent_id'];?>" class="parent_<?php echo $sub['parent_id'];?> sub" title="<?php echo $sub['name'];?>" <?php if($sub['is_checked']):?>checked<?php endif;?> value="<?php echo $sub['id'];?>">
                                        <div class="layui-unselect layui-form-checkbox" lay-skin="primary">
                                            <span><?php echo $sub['name'];?></span><i class="layui-icon"></i>
                                        </div>
                                    </span>
                                <?php endforeach;?>
                            <?php endif;?>
                        </div>
                    </td>
                </tr>
                <?php endforeach;?>
                </tbody>
            </table>
            <?php endforeach;?>
        </div>
        <input type="hidden" value="<?php echo $data['id'];?>" name="id">
        <div class="layui-form-item">
            <button class="layui-btn" type="button" lay-submit="" lay-filter="add">增加</button>
        </div>
    </form>
</div>
<script>
    var addurl = '/admin/role/up';
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;

        //监听提交
        form.on('submit(add)', function(data){
            $.post(addurl, data.field, function(res) {
                var success = JSON.parse(res);
                if(success.code===200){
                    layer.msg(success.msg, {
                        time: 1000, //不自动关闭
                        icon:6,
                        end:function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        }
                    });
                    return false;
                }else {
                    layer.msg(success.msg)
                }


            });

        });

        $(".layui-unselect").click(function () {
            var  checked = false;
            if($(this).hasClass('layui-form-checked')){
                checked = true;
            }
            var checkbox_obj = $(this).parent().find("input[type='checkbox']");
            if(checkbox_obj.hasClass('sub')){
                var parent = checkbox_obj.attr('data-parent');
                if(checked === true){
                    $("#"+parent).parent().find('div').addClass('layui-form-checked');
                    $("#"+parent).checked = true;
                } else {
                    var sub_menu = $(".parent_"+parent);
                    var sub_checked = true;
                    for(var i = 0; i< sub_menu.length; i++){
                        if($(sub_menu[i]).parent().find('div').hasClass('layui-form-checked')){
                            sub_checked = false;
                        }
                    }
                    if(sub_checked === true){
                        $("#"+parent).parent().find('div').removeClass('layui-form-checked');
                        $("#"+parent).checked = false;
                    }
                }
            } else {
                var sub_menu = $(".parent_"+checkbox_obj.attr('id'));
                for(var i = 0; i < sub_menu.length; i++){
                    if(checked === true){
                        $(sub_menu[i]).parent().find('div').addClass('layui-form-checked');
                        sub_menu[i].checked = true;
                    } else {
                        $(sub_menu[i]).parent().find('div').removeClass('layui-form-checked');
                        sub_menu[i].checked = false;
                    }

                }
            }

        })
    });



</script>

</body>

</html>